from xui.uiauto_biz import UiautoBiz
from uibiz.comm_biz import CommBiz,td
from xui.keyboard_mouse import KeyboardMouse
from htmlbiz.html_jd_iphone_id import HtmlJdIphoneId
commBiz = CommBiz()

class JdOrderCard(object):
  def __init__(this):
    this.biz= UiautoBiz()
    this.keymou = KeyboardMouse()
  # 读取苹果id信息
  def readIphoneId(this):
    window = this.biz.getEdgeWindow()
    this.biz.setActive(window)
    commBiz.sleep(500)
    this.keymou.keyEdgeSource()
    commBiz.sleep(500)
    this.keymou.keyChooseAll()
    commBiz.sleep(200)
    this.keymou.keyCopy()
    commBiz.sleep(200)
    this.keymou.keyEdgeSourceClose()
    htmlTxt = this.keymou.readClipboardTxt()
    htmlTxt = htmlTxt.encode('utf-8')
    # print(htmlTxt)
    # this.writeHtmlTxt(htmlTxt)
    txt = HtmlJdIphoneId().readIdsLs(htmlTxt)
    return txt
  # 读取订单信息列表
  def readMyOrderLs(this):
    txt = ''
    body = this.biz.getEdgeBody()
    try:
      table = body.Control(ClassName="td-void order-tb")
      trs = table.GetChildren()
      index = 0
      for tr in trs:
        domId = tr.AutomationId
        if domId.startswith('tb-'):
          # 顺序号
          t = commBiz.getIndexStr(index + 1,2)
          # 订单号
          t += td() + domId[3:]
          # 时间
          t += td() + 'time='+this.biz.readTxtByClassName(tr,'dealtime')

          trBd = tr.Control(ClassName="tr-bd")

          # 数量
          number = trBd.Control(ClassName="goods-number")
          t+= td() + 'num=' + commBiz.getNumber(number.GetFirstChildControl().Name)
          # 金额
          amount = this.biz.getChildIndex(trBd,2)
          t+= td() + '金额=' + commBiz.getNumber(amount.GetFirstChildControl().Name)

          # 商品名称
          pName = trBd.Control(ClassName="p-name")
          t += td() + 'name=' + pName.GetFirstChildControl().Name[1:]

          if txt == '':
            txt = t
          else: 
            txt += '\n'+t
          index += 1
      # print(table)
    except Exception as e:
      print(e)
      txt = '没有找到数据...'
    
    # this.biz.printChilds(table)
    # this.writeDowTxt(body)
    return txt
  def writeDowTxt(this,node):
    strLs = this.biz.printAllNode(node)
    sss = this.biz.strLsToStr(strLs)
    with open('biz_log.txt', 'w',encoding='utf-8') as f:
      f.write(sss)
  def writeHtmlTxt(this,txt):
    with open('biz_log.txt', 'w',encoding='utf-8') as f:
      f.write(txt)

